package com.lastproject.ejb.util;

import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import com.lastproject.dto.RoleDTO;
import com.lastproject.ejb.entities.Role;

/**
 * Session Bean implementation class Conversion
 */
@Stateless
@LocalBean
public class RoleConversion {

	@PersistenceContext
	EntityManager em;

	/**
	 * Default constructor.
	 */
	public RoleConversion() {
	}

	public RoleDTO fromEntity(Role r) {
		RoleDTO role = new RoleDTO();
		role.setId(r.getId());
		role.setName(r.getName());
		return role;
	}

	public Role fromDTO(RoleDTO r) {
		Role role = em.find(Role.class, r.getId());
		if (role == null) {
			role = new Role();
		}
		role.setId(r.getId());
		role.setName(r.getName());
		return role;
	}

}
